<?php

namespace Biz\model\theParagraph;

use Doraemon\pockets\publicTrait\CheckConstants;
use Doraemon\tools\ImgUrl;

class ShopBanner extends \Doraemon\pockets\datebase\BaseDbModel
{
    use CheckConstants;

    const _CONFIG_FILE_ = 'db_shop';

    const _CONFIG_SELECT_ = 'db_the_paragraph';

    const _DATABASE_ = 'the_paragraph';

    const _TABLE_NAME = 'shop_banner';

    const STATUS_NORMAL = 1;
    const STATUS_DEL = 9;


    const LOCATION_PREFIX = 'LOCATION_';
    //location
    const LOCATION_HOME_PAGE = 1;

    //showType
    const SHOW_TYPE_HOME_PAGE_TOP = 11;
    const SHOW_TYPE_HOME_PAGE_CATE_LIST = 12;

    //show type desc
    public static $showTypeDesc = [
        self::LOCATION_HOME_PAGE => [
            self::SHOW_TYPE_HOME_PAGE_TOP => '顶部轮播',
            self::SHOW_TYPE_HOME_PAGE_CATE_LIST => '品类列表',
        ],
    ];

    //location desc
    public static $locationDesc = [
        self::LOCATION_HOME_PAGE => '首页banner',
    ];

    //链接
    const LINK_TYPE_NULL = 0;
    const LINK_TYPE_GOODS_DETAIL = 1;
    const LINK_TYPE_SECOND_CATE = 2;
    const LINK_TYPE_COLLOCATION = 4;
    const LINK_TYPE_H5_PAGE = 5;
    public static $linkTypeDesc = [
        self::LINK_TYPE_GOODS_DETAIL => '商品详情页',
        self::LINK_TYPE_SECOND_CATE => '二级分类列表页',
        self::LINK_TYPE_COLLOCATION => '搭配详情',
        self::LINK_TYPE_H5_PAGE => 'h5链接',
        self::LINK_TYPE_NULL => '不跳转'
    ];

    public static $linkTypeParams = [
        self::LINK_TYPE_SECOND_CATE => "{\"category_id\":#, \"sub_category_id\":#}",
        self::LINK_TYPE_GOODS_DETAIL => "{\"goods_id\":#}",
        self::LINK_TYPE_COLLOCATION => "{\"collocation\":#}",
        self::LINK_TYPE_H5_PAGE => "h5链接",
        self::LINK_TYPE_NULL => '不跳转'
    ];

    public static function checkConstance($constantPrifix, $value)
    {
        if (self::checkConstants($constantPrifix, $value)) {
            return true;
        }
        throw new \Exception('参数错误');
    }

    public static function addData($data)
    {
        if ($data['show_type'] == self::SHOW_TYPE_HOME_PAGE_CATE_LIST) {
            $data['start_time'] = date('Y-m-d H:i:s');
            $data['stop_time'] = '2029-01-01';
        }
        self::insert([
            'title' => $data['title'],
            'location' => $data['location'],
            'show_type' => $data['show_type'],
            'banner' => $data['banner'],
            'start_time' => $data['start_time'],
            'stop_time' => $data['stop_time'],
            'admin_uid' => $data['admin_uid'],
            'add_time' => date('Y-m-d H:i:s'),
            'edit_time' => date('Y-m-d H:i:s'),
            'weight' => $data['weight'],
            'link_type' => $data['link_type'],
            'link_des' => $data['link_des'],
            'status' => self::STATUS_NORMAL,
        ]);
        return self::id();
    }

    public static function editData($bannerId, $data)
    {
        if ($data['show_type'] == self::SHOW_TYPE_HOME_PAGE_CATE_LIST) {
            $data['start_time'] = date('Y-m-d H:i:s');
            $data['stop_time'] = '2029-01-01';
        }
        return self::update([
            'title' => $data['title'],
            'location' => $data['location'],
            'show_type' => $data['show_type'],
            'banner' => $data['banner'],
            'start_time' => $data['start_time'],
            'stop_time' => $data['stop_time'],
            'admin_uid' => $data['admin_uid'],
            'edit_time' => date('Y-m-d H:i:s'),
            'weight' => $data['weight'],
            'link_type' => $data['link_type'],
            'link_des' => $data['link_des'],
        ], ['id' => $bannerId])->rowCount();

    }

    public static function admselectInfoByLocation($where)
    {
        $bannerList = self::select('*', $where);
        $i = 0;
        $pageNum = 10;
        $rowNum = 10 / 2;
        $count = count($bannerList);
        foreach ($bannerList as $k => $banner) {
            $info = self::admformatBanner($banner);
            if (isset($where['show_type']) && $where['show_type'] == self::SHOW_TYPE_HOME_PAGE_CATE_LIST) {
                $pageStr = (int)($i / $pageNum) + 1;
                if ($i < $pageNum) {
                    $rowStr = (int)($i / $rowNum) + 1;
                } else {
                    $rowStr = (int)(($i - $pageNum) / $rowNum) + 1;
                }
                if (($i + 1) % $rowNum == 0) {
                    $columnStr = $rowNum;
                } else {
                    $columnStr = ($i + 1) % $rowNum;
                }
                if (($pageStr * $pageNum) > ($pageStr - 1) * $pageNum && ($pageStr * $pageNum) > $count) {
                    $pageNum2 = $count % $pageNum;
                    $j = $i - (($pageStr - 1) * $pageNum);
                    $rowNum2 = ceil($pageNum2 / 2);
                    if ($j < $pageNum2) {
                        $rowStr = (int)($j / $rowNum2) + 1;
                    } else {
                        $rowStr = (int)(($j - $rowNum2) / $rowNum2) + 1;
                    }
                    if (($j + 1) % $rowNum2 == 0) {
                        $columnStr = $rowNum2;
                    } else {
                        $columnStr = ($j + 1) % $rowNum2;
                    }
                }
                $info['show_table'] = '第' . $pageStr . '屏-' . $rowStr . '行-' . $columnStr . '位';
            }
            $bannerList[$k] = $info;
            $i++;
        }

        return $bannerList;
    }

    public static function admformatBanner($banner)
    {
        $banner['location_desc'] = self::$locationDesc[$banner['location']];
        $banner['show_type_desc'] = self::$showTypeDesc[$banner['location']][$banner['show_type']];
        $banner['link_type_desc'] = self::$linkTypeDesc[$banner['link_type']];
        $banner['banner_url'] = ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $banner['banner'], ImgUrl::IMAGE_SIZE_SQ_SMALL);
        return $banner;
    }

    public static function getCurrentBannerList($location, array $showTypes)
    {
        $currentTime = date('Y-m-d H:i:s');
        $list = self::select('*', [
            'location' => $location,
            'show_type' => $showTypes,
            'start_time[<=]' => $currentTime,
            'stop_time[>=]' => $currentTime,
            'status' => self::STATUS_NORMAL,
            'ORDER' => [
                'weight' => 'DESC'
            ]
        ]);
        $brannerList = [];
        foreach ($list as $item) {
            $brannerList[$item['location']][$item['show_type']][] = $item;
        }
        return $brannerList;
    }

    //获取 未开始的第一个banner  计算 banner 缓存需要
    public static function getNoActionBanner($location, $showType, $dateTime)
    {
        return self::get('*', [
            'location' => $location,
            'show_type' => $showType,
            'start_time[>]' => $dateTime,
            'status' => self::STATUS_NORMAL,
            'ORDER' => ['start_time' => 'ASC']
        ]);

    }
}
